package br.com.klis.batendoumabola.server;

import java.io.IOException;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.blobstore.BlobKey;
import com.google.appengine.api.blobstore.BlobstoreService;
import com.google.appengine.api.blobstore.BlobstoreServiceFactory;

@SuppressWarnings("serial")
public class UploadServlet extends HttpServlet {
    //private static final Logger log = Logger.getLogger(UploadServlet.class.getName());

	
    private BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();    

    public void doPost(HttpServletRequest req, HttpServletResponse res)
        throws ServletException, IOException {

    	int len = req.getContentLength();
    	int mb = (1024 * 1024) * 1;
    	if (len > mb) {
    		throw new RuntimeException("Sorry that file is to large. Try < 1024 or 1MB file");  
    	}
    	
        Map<String, BlobKey> blobs = blobstoreService.getUploadedBlobs(req);
        BlobKey blobKey = blobs.get("uploadFormElement");
       
        if (blobKey == null) {

        } else {

        	
 /*       	ImagesService imagesService = ImagesServiceFactory.getImagesService();
        	String imageUrl = imagesService.getServingUrl(blobKey);

        	UserService userService = UserServiceFactory.getUserService();
            // TODO: Add a better check for whether the user is logged in or not
        	// Don't even let the user upload or get here
        	User user = userService.getCurrentUser();
        	
        	Entity uploadedImage = new Entity("UploadedImage");
        	uploadedImage.setProperty("blobKey", blobKey);
        	uploadedImage.setProperty(UploadedImage.CREATED_AT, new Date());
        	uploadedImage.setProperty(UploadedImage.OWNER_ID, user.getUserId());
        	
        	// Highly unlikely we'll ever search on this property
        	uploadedImage.setUnindexedProperty(UploadedImage.SERVING_URL, imageUrl);
        	
        	DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
        	datastore.put(uploadedImage);
        	
        	String keyString = KeyFactory.keyToString(uploadedImage.getKey());
            res.sendRedirect("/upload?uploadedImageKey=" + keyString);        */
        	res.sendRedirect("/upload?blobKey=" + blobKey.getKeyString());
        }
    }
    
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
    		throws ServletException, IOException {
    	
    	
    	/*String uploadedImageKey = req.getParameter("blobKey");
    	resp.setHeader("Content-Type", "text/html"); 
    	
    	
    	// This is a bit hacky, but it'll work. We'll use this key in an Async service to
    	// fetch the image and image information
    	resp.getWriter().println(uploadedImageKey);*/
    	
    	
    	BlobKey blobKey = new BlobKey(req.getParameter("blobKey"));
    	blobstoreService.serve(blobKey, resp);
    
    }
}
